package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.PageData;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.controller.vo.CommentView;
import com.javaweb.blogcite.pojo.Comment;
import com.javaweb.blogcite.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class CommentController {
    @Resource
    private CommentService commentService;

    @GetMapping("/api/comment")
    public ResponseResult<PageData<CommentView>> getAllComment(Integer page, Integer limit, Integer blogid) {
        PageData<CommentView> commentPage = commentService.getCommentByPage(page, limit, blogid);
        ResponseResult<PageData<CommentView>> responseResult = new ResponseResult<>();
        responseResult.setCode(0);
        responseResult.setMsg("success");
        responseResult.setData(commentPage);
        return responseResult;
    }

    @JwtCheck
    @DeleteMapping("/api/comment/{id}")
    public ResponseResult deleteComment(@PathVariable ("id") Integer id) {
        boolean success = commentService.deleteComment(id);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("删除数据失败！");
        }
        return result;
    }

    @PostMapping("/api/comment")
    public ResponseResult<CommentView> setComment(@RequestBody Comment comment) {
        CommentView cmt = commentService.setComment(comment);
        ResponseResult<CommentView> result = new ResponseResult<>();
        if (cmt != null) {
            result.setCode(0);
            result.setMsg("");
            result.setData(cmt);
        } else {
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }
}
